﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using EazyCMS.DBUtility;
using EazyCMS.Model;

namespace EazyCMS.DAL
{
    public partial class tools_helper
    {
        private string databaseprefix; //数据库表名前缀
        public tools_helper(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }
        #region 访问统计的基本方法================================
        /// <summary>
        /// 向Ip地址信息库中添加数据
        /// </summary>
        /// <returns></returns>
        public int insert_visit_num(visit_num ipinfo)
        {
            string sql = "insert into " + databaseprefix + "visit_num (IPadress,logintime)values('" + ipinfo.iPadress + "','" + ipinfo.logintime + "')";
            return DbHelperSQL.ExecuteSql(sql);
        }
        /// <summary>
        /// 根据Ip地址进行查询,返回查询结果
        /// </summary>
        /// <returns></returns>
        public int select_visit_byip(string ipadress)
        {
            string sql = "select COUNT(*) from " + databaseprefix + "visit_num where IPadress='" + ipadress + "'";
            return Convert.ToInt32(DbHelperSQL.GetSingle(sql));
        }
        /// <summary>
        /// 根据id更新ip记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int update_visit_num(visit_num ipinfo)
        {
            string sql = "update " + databaseprefix + "visit_num set logintime='" + ipinfo.logintime + "' where id='" + ipinfo.id + "'";
            return DbHelperSQL.ExecuteSql(sql);

        }
        //根据IP地址取得相关信息
        public visit_num get_visit_byip(string ip)
        {
            visit_num ipinfo = new visit_num();
            string sql = "select * from " + databaseprefix + "visit_num where IPadress='" + ip + "'";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            ipinfo.id = Convert.ToInt32(dt.Rows[0]["id"]);
            ipinfo.iPadress = dt.Rows[0]["IPadress"].ToString();
            ipinfo.logintime = Convert.ToDateTime(dt.Rows[0]["logintime"]);
            return ipinfo;



        }
        /// <summary>
        /// 如果该IP地址信息时间不在今天范围内则删除该ip地址相关信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delete_visit_num(int id)
        {
            string sql = "delete from " + databaseprefix + "visit_num where id=" + id;
            return DbHelperSQL.ExecuteSql(sql);
        }
        /// <summary>
        /// 清空ip信息数据库
        /// </summary>
        public void delete_visit_all(DateTime d1, DateTime d2)
        {
            string sql = "delete from " + databaseprefix + "visit_num where logintime between '" + d1 + "' and '" + d2 + "' ";
            DbHelperSQL.ExecuteSql(sql);

        }
        ///-------------------更新访问数据----------------------////

        ///更新访问数据
        /// 如果是当天的总数和当天统计数都累加，反之不在当天范围内  总数累加  当天访问量置为1
        /////
        public void updatenum()
        {
            //首先读取数据库中的时间
            string sql = "select dtime from " + databaseprefix + "total_num where id=1";
            //得到当前时间
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            DateTime jintstar = Convert.ToDateTime(year + "-" + month + "-" + day + " 00:00:01");
            DateTime jintend = Convert.ToDateTime(year + "-" + month + "-" + day + " 23:59:59");
            DateTime realtime = Convert.ToDateTime(DbHelperSQL.GetSingle(sql));
            if (realtime > jintstar && realtime < jintend)
            {
                //当天
                string sqlupdateone = "update " + databaseprefix + "total_num set totalnum=totalnum+1,todaynum=todaynum+1 where id=1";
                DbHelperSQL.ExecuteSql(sqlupdateone);
            }
            else
            {
                DateTime currenttime = DateTime.Now;
                //不在当天范围内
                string sqlupdate = "update " + databaseprefix + "total_num set totalnum=totalnum+1,todaynum=1,dtime='" + currenttime + "' where id=1";
                DbHelperSQL.ExecuteSql(sqlupdate);

            }


        }

        #endregion
    }
}
